import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Deployment } from './entities/deployment.entity';
import { CreateDeploymentDto } from './dto/create-deployment.dto';
import { UpdateDeploymentDto } from './dto/update-deployment.dto';

@Injectable()
export class DeploymentService {
  constructor(
    @InjectRepository(Deployment)
    private deploymentRepository: Repository<Deployment>,
  ) {}

  async create(createDeploymentDto: CreateDeploymentDto): Promise<Deployment> {
    const deployment = this.deploymentRepository.create(createDeploymentDto);
    return this.deploymentRepository.save(deployment as Deployment);
  }

  async findAll(): Promise<Deployment[]> {
    return this.deploymentRepository.find({
      order: { createdAt: 'DESC' },
    });
  }

  async findOne(id: number): Promise<Deployment> {
    return this.deploymentRepository.findOne({ where: { id } });
  }

  async update(id: number, updateDeploymentDto: UpdateDeploymentDto): Promise<Deployment> {
    await this.deploymentRepository.update(id, updateDeploymentDto);
    return this.findOne(id);
  }

  async remove(id: number): Promise<void> {
    await this.deploymentRepository.delete(id);
  }
} 